import 'dart:convert' as convert;
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_web_study/api/dio_utils.dart';
import 'package:flutter_web_study/utils/toast_utils.dart';

import '../api/service_api.dart';
import '../bean/menu_item.dart';
import '../router/application.dart';
import '../router/routes.dart';

class SearchViewModel extends ChangeNotifier {
  TextEditingController textEditingController = TextEditingController();

  List list = [];
  String? tagPage;
  String? brand = "";
  String? series = "";
  BuildContext me;

  SearchViewModel(this.me, this.tagPage, this.brand, this.series);

  initView() {
    print("====$tagPage====");
    if (tagPage == "0") {
      DioUtils.getInstance()
          ?.post(ServiceApi.FUZZY_QUERY_MAKEUP_GOODS_BY_BRAND, {"name": ""},
              (successCallBack) {
        list = (successCallBack["data"]);
        notifyListeners();
      }, (errorCallBack) {});
    } else if (tagPage == "1") {
      DioUtils.getInstance()?.post(ServiceApi.FUZZY_QUERY_MAKEUP_GOODS_SERIES,
          {"series": "", "brand": brand}, (successCallBack) {
        list = (successCallBack["data"]);
        notifyListeners();
      }, (error) {});
    } else if (tagPage == "2") {
      DioUtils.getInstance()?.post(ServiceApi.FUZZY_QUERY_MAKEUP_GOODS_COLOR,
          {"color": "", "brand": brand, "series": series}, (successCallBack) {
        list = (successCallBack["data"]);
        notifyListeners();
      }, (error) {});
    }
  }

  //搜索返回
  finishValue() {
    Navigator.pop(me, {"name": textEditingController.text});
  }

  getViewData(value) {
    if (tagPage == "0") {
      DioUtils.getInstance()
          ?.post(ServiceApi.FUZZY_QUERY_MAKEUP_GOODS_BY_BRAND, {"name": value},
              (successCallBack) {
        list = (successCallBack["data"]);
        notifyListeners();
      }, (errorCallBack) {});
    } else if (tagPage == "1") {
      DioUtils.getInstance()?.post(ServiceApi.FUZZY_QUERY_MAKEUP_GOODS_SERIES,
          {"series": value, "brand": brand}, (successCallBack) {
        list = (successCallBack["data"]);
        notifyListeners();
      }, (error) {});
    } else if (tagPage == "2") {
      DioUtils.getInstance()?.post(ServiceApi.FUZZY_QUERY_MAKEUP_GOODS_COLOR, {
        "color": value,
        "brand": brand,
        "series": series
      }, (successCallBack) {
        list = (successCallBack["data"]);
        notifyListeners();
      }, (error) {});
    }
  }

  selectListValue(index) {
    if (tagPage == "0") {
      Navigator.pop(me, {"tag": list[index]});
    } else if (tagPage == "1") {
      Navigator.pop(me, {"name": list[index]});
    } else if (tagPage == "2") {
      Navigator.pop(me, {"name": list[index]});
    }
  }
}
